package com.ph.website.home.service.impl;

import com.ph.website.home.base.common.CommonResult;
import com.ph.website.home.base.common.Ref;
import com.ph.website.home.base.component.GlobalParam;
import com.ph.website.home.base.utils.TimeUtil;
import com.ph.website.home.entity.HomeImg;
import com.ph.website.home.mapper.HomeImgMapper;
import com.ph.website.home.param.HomeImgParam;
import com.ph.website.home.service.IHomeImgService;
import com.ph.website.home.vo.HomeImgVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class HomeImgServiceImpl implements IHomeImgService {


    @Resource
    private HomeImgMapper homeImgMapper;
    @Resource
    private GlobalParam globalParam;

    /**获取所有的轮播图*/
    public CommonResult listAll(){

        List<HomeImg> list = homeImgMapper.listAll();
        List<HomeImgVo.ListAllVo> vos = new ArrayList<>();

        for(HomeImg img : list){
            HomeImgVo.ListAllVo vo = new HomeImgVo.ListAllVo();
            vo.setImgId(img.getId());
            vo.setImgUrl(img.getImgUrl());
            vo.setTitle(img.getTitle());
            vo.setSubTitle1(img.getSubTitle1());
            vo.setSubTitle2(img.getSubTitle2());
            vo.setSubTitle3(img.getSubTitle3());
            vo.setOrderNum(img.getOrderNum());
            vo.setTime(TimeUtil.formaterYYYYMMddHHmm(img.getCreateTime()));
            vo.setUsable(img.getUsable());
            vo.setHasLink(img.getHasLink());
            vo.setLinkUrl(img.getLinkUrl());
            vo.setPrefix(globalParam.getImg().getPrefix());
            vo.setImgViewUrl(globalParam.getImg().getPrefix()+img.getImgUrl());
            vos.add(vo);
        }
        return Ref.ok(vos);
    }


    /**添加轮播图*/
    public CommonResult add(HomeImgParam.AddParam param){
        HomeImg homeImg = new HomeImg();
        homeImg.setCreateTime(new Date());
        homeImg.setHasLink(param.getHasLink());
        homeImg.setImgUrl(param.getImgUrl());
        homeImg.setLinkUrl(param.getLinkUrl());
        homeImg.setOrderNum(param.getOrderNum());
        homeImg.setUsable(param.getUsable());
        homeImg.setTitle(param.getTitle());
        homeImg.setSubTitle1(param.getSubTitle1());
        homeImg.setSubTitle2(param.getSubTitle2());
        homeImg.setSubTitle3(param.getSubTitle3());

        homeImgMapper.insert(homeImg);
        return Ref.ok();
    }

    /**修改轮播图*/
    public CommonResult modify(HomeImgParam.ModifyParam param){
        HomeImg img = homeImgMapper.selectById(param.getImgId());
        if(img==null){
            return Ref.error("错误的轮播图编号");
        }
        img.setUsable(param.getUsable());
        img.setOrderNum(param.getOrderNum());
        img.setLinkUrl(param.getLinkUrl());
        img.setImgUrl(param.getImgUrl());
        img.setHasLink(param.getHasLink());
        img.setTitle(param.getTitle());
        img.setSubTitle1(param.getSubTitle1());
        img.setSubTitle2(param.getSubTitle2());
        img.setSubTitle3(param.getSubTitle3());
        homeImgMapper.updateById(img);
        return Ref.ok();
    }

    /**删除轮播图*/
    public CommonResult delete(HomeImgParam.DeleteParam param){
        homeImgMapper.deleteById(param.getImgId());
        return Ref.ok();
    }
}
